package com.bjsxt.shop.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.users.dao.IAAdminsDao;
import com.bjsxt.shop.users.dao.IARoleDao;
import com.bjsxt.shop.users.pojo.AAdmins;
import com.bjsxt.shop.users.pojo.ARole;
import com.bjsxt.shop.users.service.IUsersService;
@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap)
	{
		
		return this.adminsDao.findOne(condMap);
	}
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.save(admins);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data",dataJSON);
		}else{
			resultJSON.put("code","1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.update(admins);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", admins.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data",dataJSON);
		}else{
			resultJSON.put("code","2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.adminsDao.delete(admins);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id",admins.getId()+"");
			dataJSON.put("effect",res+"");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code","3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		if(pageInfoUtil != null){
			condMap.put("pageCount", "true");
			List<AAdmins> roleList = this.adminsDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			condMap.put("pageCount", "false");
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminsDao.findCondList(condMap);
		}
		return this.adminsDao.findCondList(condMap);
	}
		
	
	
	
	
	
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap)
	{
		
		return this.roleDao.findOne(condMap);
	}
	@Override
	public JSONObject saveOneRoleService(ARole Role)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.roleDao.save(Role);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", Role.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data",dataJSON);
		}else{
			resultJSON.put("code","1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
			
			
		}
		return resultJSON;
	}
	@Override
	public JSONObject updateOneRoleService(ARole Role)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.roleDao.update(Role);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id", Role.getId()+"");
			dataJSON.put("effect", res+"");
			resultJSON.put("data",dataJSON);
		}else{
			resultJSON.put("code","2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneRoleService(ARole Role)
	{
		JSONObject resultJSON=new JSONObject();
		int res=this.roleDao.delete(Role);
		if(res>0){
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
			JSONObject dataJSON=new JSONObject();
			dataJSON.put("id",Role.getId()+"");
			dataJSON.put("effect",res+"");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code","3");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("3"));
		}
		return resultJSON;
	}
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condMap.put("pageCount", "true");
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageInfoUtil.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condMap.put("pageCount", "false");
			/* limit ? , ? */
			condMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			condMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.findCondList(condMap);
		}
		/* 不分页查询数据 */
		return this.roleDao.findCondList(condMap);
	}
		
	
}
